package com.dime.volumnwidget;

import android.content.Context;
import android.media.AudioManager;
import android.net.rtp.AudioStream;

public class VolumnUtils {

    Context mContext;
    AudioManager audioManager;
    int maxVolumn, minVolumn, currentVolumn;

    public VolumnUtils(Context context) {
        this.mContext = context;
        init();
    }

    public int getCurrentVolumn(){
        return currentVolumn;
    }

    private void init() {
        audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        maxVolumn = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        minVolumn = audioManager.getStreamMinVolume(AudioManager.STREAM_MUSIC);
        currentVolumn = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    }


    public int volumnUp() {
        int newVolumn = currentVolumn + 1;
        return setVolumn(newVolumn);
    }

    public int volumnDown() {
        int newVolumn = currentVolumn - 1;
        return setVolumn(newVolumn);
    }

    public int volumnMute() {
        return setVolumn(0);
    }

    public int setVolumn(int newVolumn) {
        if (audioManager == null) {
            init();
        }
        if (newVolumn >= 0 && newVolumn <= maxVolumn) {
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newVolumn, 0);
            currentVolumn = newVolumn;
        }
        return currentVolumn;
    }
}
